import { Application } from 'egg';
import { DataTypes, Model } from 'sequelize';

export interface ExclusiveDishCategoryAttributes {
  id?: number;
  restaurant_id: number;
  name: string;
  icon: string;
  sort_order?: number;
  created_at?: Date;
  updated_at?: Date;
}

export interface ExclusiveDishCategoryInstance extends Model<ExclusiveDishCategoryAttributes>, ExclusiveDishCategoryAttributes {}

export default (app: Application) => {
  const { INTEGER, STRING, DATE } = DataTypes;

  const ExclusiveDishCategory = app.model.define<ExclusiveDishCategoryInstance>('ExclusiveDishCategory', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    restaurant_id: { type: INTEGER, allowNull: false, comment: '专属餐厅ID' },
    name: { type: STRING(50), allowNull: false, comment: '分类名称' },
    icon: { type: STRING(100), allowNull: true, comment: '分类图标' },
    sort_order: { type: INTEGER, allowNull: true, defaultValue: 0, comment: '排序' },
    created_at: { type: DATE, allowNull: false, defaultValue: DataTypes.NOW },
    updated_at: { type: DATE, allowNull: false, defaultValue: DataTypes.NOW },
  }, {
    tableName: 'exclusive_dish_categories',
    comment: '专属餐厅菜品分类表',
  });

  (ExclusiveDishCategory as any).associate = () => {
    app.model.ExclusiveDishCategory.belongsTo(app.model.ExclusiveRestaurant, { foreignKey: 'restaurant_id', as: 'restaurant' });
    app.model.ExclusiveDishCategory.hasMany(app.model.ExclusiveDish, { foreignKey: 'category_id', as: 'dishes' });
  };

  return ExclusiveDishCategory;
};